package ctr.layer;

import java.util.ArrayList;

import connection.layer.WCFConnection;
import main.foodanddallergy.R;
import model.layer.Product;
import model.layer.Recipe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class CtrRecipe extends Activity {
	
	private ArrayAdapter<String> recipeAA;
	private ArrayList<Recipe> tempList;
	private ArrayList<Recipe> recipeList = new ArrayList<Recipe>();
	private ArrayList<String> recipeStrings = new ArrayList<String>();
	private FoodAndAllergy fa;
	EditText searchfield;
	WCFConnection wcf = new WCFConnection();
	
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe);
        
        Button searchBtn = (Button)findViewById(R.id.searchbuton);
        searchfield = (EditText)findViewById(R.id.searchrecipe);
        ListView recipes = (ListView)findViewById(R.id.recipelist);
        
        recipes.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Recipe _recipe = recipeList.get(position);
				fa = (FoodAndAllergy)getApplication();
				fa.addRecipe(_recipe);
				Intent startRecipe = new Intent(CtrRecipe.this, CtrShowRecipe.class);
				startActivity(startRecipe);
			}
		});
      
        
        recipeAA = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, recipeStrings);
        recipes.setAdapter(recipeAA);

        
        searchBtn.setOnClickListener(new OnClickListener() { 
			
			public void onClick(View v) {
				//searchRecipeByName();
				testRecipe();
			}
		});
        
        
        	 
        
	}
	
	private void testRecipe()
	{
		Recipe tRep = new Recipe();
		
		tRep.setId(1);
		tRep.setName("Pandekager");
		tRep.setServings(4);
		tRep.setTypeOf("Dessert");
		
		
		Product milk = new Product();
		milk.setId(1);
		milk.setAmount(2.5);
		milk.setUnit("liter");
		
		Product egg = new Product();
		egg.setId(2);
		egg.setName("Egg");
		egg.setAmount(5);
		egg.setUnit("styks");
		
		tRep.addIngredients(milk);
		tRep.addIngredients(egg);
		
		recipeList.add(tRep);
		recipeAA.add(tRep.getName());
		recipeAA.notifyDataSetChanged();
		
	}
	private void searchRecipeByName()
	{
		String name = searchfield.getText().toString().trim();
        
		tempList = new ArrayList<Recipe>();
		
		tempList = wcf.GetRecipeByName(name);
		
		for (Recipe res : tempList) {
			
			recipeList.add(res);
			recipeAA.add(res.getName());
			recipeAA.notifyDataSetChanged();
		}
		
		
		
		
	}
}
